from abc import ABC

from mashangpa.responce_parse.baseParser import BaseParser
import datetime
import hashlib
import json

class OneQuestionParser(BaseParser, ABC):

    def parse_responce(self, response):
        print(response)
        return response

    def encryption_param(self, page: int) -> dict:
        return {"page": page}

class TwoQuestionParser(BaseParser, ABC):

    def parse_responce(self, response):
        return response

    def encryption_param(self, page: int) -> dict:
        return {"page": page}

class ThreeQuestionParser(BaseParser, ABC):

    def parse_responce(self, response):
        # 参考博客：https://www.cnblogs.com/Eeyhan/p/15410251.html
        return response

    def encryption_param(self, page: int) -> dict:
        return {"page": page}

class FourQuestionParser(BaseParser, ABC):
    def parse_responce(self, response):
        print(response)
        return response

    def encryption_param(self, page: int) -> dict:
        param = {}
        param["page"] = page
        timestamp = round(datetime.datetime.now().timestamp() * 1000)
        sign = hashlib.md5(f"tuling{timestamp}{page}".encode("utf-8")).hexdigest()
        param["sign"] = sign
        param["_ts"] = timestamp
        return param

class FiveQuestionParser(BaseParser, ABC):
    method = "POST"

    def parse_responce(self, response):
        print(response)
        return response

    def encryption_param(self, page: int) -> dict:
        param = {}
        param["page"] = page
        timestamp = round(datetime.datetime.now().timestamp() * 1000)
        param["_ts"] = timestamp

        #参数加密
        js_context = self.init_js_context('encrypt1')
        encrypted = js_context.call('encrypt', json.dumps(param))
        param = {'xl': encrypted}
        return param

class SixQuestionParser(BaseParser, ABC):
    def parse_responce(self, response):
        result_data = self.js_context.call('decrypt', response['t'])
        return json.loads(result_data)

    def encryption_param(self, page: int) -> dict:
        self.js_context = self.init_js_context('encrypt6')
        self.Headers = self.js_context.call('get_HeadersDict')

        return {"page": page}

class SevenQuestionResponseParser(BaseParser, ABC):
    def parse_responce(self, response):
        result_data = self.js_context.call('decrypt', response['r'])
        return result_data

    def encryption_param(self, page: int) -> dict:
        # self.js_context = self.init_js_context('encrypt7')
        self.js_context = self.init_js_context('encrypt7')
        encrypt_data = self.js_context.call('encrypt', page)
        print('encrypt_data', encrypt_data)
        self.Headers = encrypt_data['Header']
        return encrypt_data['param']

class EightQuestionResponseParser(BaseParser, ABC):
    method = "POST"

    def parse_responce(self, response):
        return response

    def encryption_param(self, page: int) -> dict:
        self.js_context = self.init_js_context('encrypt8')
        encrypt_data = self.js_context.call('encrypt', page)
        self.Headers = encrypt_data['Header']
        self.Cookies = encrypt_data['Cookie']
        return encrypt_data['param']

class NineQuestionResponseParser(BaseParser, ABC):
    method = "POST"
    def parse_responce(self, response):
        return response

    def encryption_param(self, page: int) -> dict:
        self.js_context = self.init_js_context('encrypt9')
        encrypt_data = self.js_context.call('encrypt', page)
        return encrypt_data['param']

class TenQuestionResponseParser(BaseParser, ABC):
    def parse_responce(self, response):
        return response

    def encryption_param(self, page: int) -> dict:
        self.js_context = self.init_js_context('encrypt10')
        encrypt_data = self.js_context.call('encrypt', page)
        return encrypt_data['param']

